import {Component, OnDestroy, OnInit, ViewChild, ViewEncapsulation} from '@angular/core';
import {ActivatedRoute, Params} from "@angular/router";
import {Observable} from "rxjs";

import {ToolsService} from "@shared/service/tools/tools.service";
import {ToolbarService} from "@shared/components/toolbar/toolbar.service";
import {FeedService} from "@feed/feed.service";
import {Subject} from "rxjs/Subject";
import {SeedInfiniteScroll} from "@shared/components/infinite-scroll/seed-infinite-scroll/seed-infinite-scroll.component";

// import {FeedRespVO} from "@interface/interface/interface.model";
// import {CommonPageVo} from "@interface/shared/shared.model";
import {AuthService} from "@shared/service/auth/auth.service";
import {FeedRespVO} from "@interface/index";

@Component({
  selector: 'seed-feed',
  templateUrl: './feed.component.html',
  styleUrls: ['./feed.component.scss'],
  encapsulation: ViewEncapsulation.None,
})
export class FeedComponent implements OnInit {
  @ViewChild(SeedInfiniteScroll) infiniteScroll;
  public feeds: FeedRespVO[] = [];
  public stateStore = {
    haveData: true
  };
  public dataStore = {
    pageTitle: '帖子'
  };
  public params = {
    userId: '',
    page: 1,
    rows: 10
  };
  public ngUnsubscribe: Subject<void> = new Subject<void>();

  constructor(private _feedService: FeedService,
              private _authService: AuthService,
              public toolsService: ToolsService,
              private _route: ActivatedRoute) {
    this.handleFeedRequest();
  }

  handleFeedRequest() {
    this._route.params.subscribe((params: Params) => {
      if (params['userId']) {
        this.params.userId = params['userId'];
      } else {
        this.dataStore.pageTitle = '我的帖子';
        this.params.userId = this._authService.getUserId();
      }
      this.getFeeds();
    });
  }

  // /**
  //  * @description 获取我点过赞的帖子
  //  * @author ouyqx-691
  //  * @date 2018-1-19
  //  */
  // getFeedDigg() {
  //   this._feedService.getFeedDigg(this.params)
  //     .takeUntil(this.ngUnsubscribe).subscribe(res => {
  //     this.toolsService.hideLoading();
  //     if (res.result === '0') {
  //       this.params.page++;
  //       if (this.infiniteScroll) {
  //         this.infiniteScroll.complete();
  //       }
  //       if (!res.data.hasNextPage && this.infiniteScroll) {
  //         this.infiniteScroll.enable(false);
  //       }
  //     }
  //   });
  // }

  getFeeds() {
    this._feedService.getFeeds(this.params).subscribe(res => {
      if (res.result === '0' && res.data) {
        this.params.page++;
        const feeds = res.data.list;
        this.infiniteScroll.complete();
        this.feeds = [...this.feeds, ...feeds];
        if (!res.data.hasNextPage) {
          if (feeds.length === 0) {
            this.stateStore.haveData = false;
          }
          this.infiniteScroll.enable(false);
        }
      }
    });
  }

  ngOnInit() {

  }

}
